package intranet.controllers;

import intranet.addon.Enscription;
import intranet.models.LoginModel;
import intranet.models.MyGroups;
import intranet.models.RegistrationModel;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class LoginController extends HttpServlet {

	//Save Groups
	@RequestMapping("/SaveGroups.html")
	public ModelAndView UdateGroup(HttpServletRequest request, HttpServletResponse response) throws SQLException,ServletException, IOException{
		String gname= request.getParameter("groupname");
		String gid= request.getParameter("groupid");
		String member= request.getParameter("txtmembers");
		ModelAndView mvt=new ModelAndView();
		MyGroups myg=new MyGroups();
		myg.UpdateGroup(gname,gid,member);
		ModelAndView mv=new ModelAndView("index");
		response.sendRedirect("mygroups.html");
		return mv;
		
		}
	//Add New Groups
	@RequestMapping("/NewGroups.html")
	public ModelAndView NewGroup(HttpServletRequest request, HttpServletResponse response) throws SQLException,ServletException, IOException{
		String gname= request.getParameter("groupname");
		ModelAndView mvt=new ModelAndView();
		MyGroups myg=new MyGroups();
		myg.NewGroup(gname);
		
		ModelAndView mv=new ModelAndView("index");
		return mv;
		}
	   public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{
		    ResultSetMetaData md = rs.getMetaData();
		    int columns = md.getColumnCount();
		    ArrayList results = new ArrayList();
		    while (rs.next()){
		    HashMap row = new HashMap();
		    results.add(row);
			    for(int i=1; i<=columns; i++){
			    row.put(md.getColumnName(i),rs.getObject(i));
			    }
		    }
		    return results;

	    }
}